package com.codingvip.queue;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class MovingAverage {
    List<Integer> queue = null;
    int maxSize;


    /**
     * Initialize your data structure here.
     */
    public MovingAverage(int size) {
        queue = new ArrayList<>();
        maxSize = size;
    }


    public double next(int val) {
        queue.add(val);
        int beginIndex=Math.max(0,queue.size() - maxSize);
        int sum = 0;
        for (int i = beginIndex; i < queue.size(); i++) {
            sum += queue.get(i);
        }
        return sum * 1.0 / Math.min(queue.size(), maxSize);
    }

    public static void main(String[] args) {
        MovingAverage movingAverage = new MovingAverage(3);

        System.out.println(movingAverage.next(1));
        System.out.println(movingAverage.next(10));
        System.out.println(movingAverage.next(3));
        System.out.println(movingAverage.next(5));

    }
}